<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java并发编程：JUC核心概念与实践</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', 'Noto Serif SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background-color: #282c34;
            border-radius: 8px;
            padding: 1.5rem;
            color: #abb2bf;
            font-family: 'Courier New', Courier, monospace;
            overflow-x: auto;
        }
        .code-keyword {
            color: #c678dd;
        }
        .code-type {
            color: #e5c07b;
        }
        .code-string {
            color: #98c379;
        }
        .code-comment {
            color: #5c6370;
            font-style: italic;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
        }
        .concept-diagram {
            background-color: #f8f9fa;
            border-radius: 12px;
            padding: 2rem;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <header class="hero-gradient text-white py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-6xl mx-auto">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-4 leading-tight">Java并发编程<br>JUC核心概念与实践</h1>
                    <p class="text-xl mb-8 opacity-90">掌握Java并发工具包(JUC)的核心组件与最佳实践，构建高性能、高并发的应用程序</p>
                    <div class="flex space-x-4">
                        <a href="#juc-intro" class="bg-white text-indigo-700 px-6 py-3 rounded-lg font-medium hover:bg-indigo-50 transition duration-300">
                            <i class="fas fa-book-open mr-2"></i>开始学习
                        </a>
                        <a href="#juc-components" class="border-2 border-white text-white px-6 py-3 rounded-lg font-medium hover:bg-white hover:text-indigo-700 transition duration-300">
                            <i class="fas fa-cubes mr-2"></i>核心组件
                        </a>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="bg-white bg-opacity-20 p-6 rounded-2xl backdrop-filter backdrop-blur-sm">
                        <div class="code-block">
                            <span class="code-keyword">public class</span> <span class="code-type">ConcurrentApp</span> {<br>
                            &nbsp;&nbsp;<span class="code-keyword">public static void</span> <span class="code-type">main</span>(String[] args) {<br>
                            &nbsp;&nbsp;&nbsp;&nbsp;<span class="code-type">ExecutorService</span> executor = <span class="code-type">Executors</span>.newFixedThreadPool(4);<br>
                            &nbsp;&nbsp;&nbsp;&nbsp;<span class="code-type">ConcurrentHashMap</span>&lt;String, Integer> map = <span class="code-keyword">new</span> <span class="code-type">ConcurrentHashMap</span>&lt;>();<br>
                            &nbsp;&nbsp;&nbsp;&nbsp;<span class="code-comment">// 并发处理任务</span><br>
                            &nbsp;&nbsp;&nbsp;&nbsp;<span class="code-keyword">for</span> (int i = 0; i < 10; i++) {<br>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;executor.submit(() -> {<br>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map.put(<span class="code-string">"key"</span>, <span class="code-type">ThreadLocalRandom</span>.current().nextInt(100));<br>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});<br>
                            &nbsp;&nbsp;&nbsp;&nbsp;}<br>
                            &nbsp;&nbsp;&nbsp;&nbsp;executor.shutdown();<br>
                            &nbsp;&nbsp;}<br>
                            }
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-16">
        <!-- Introduction Section -->
        <section id="juc-intro" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-600 flex-1"></div>
                <h2 class="text-3xl font-bold px-4">JUC基础与核心概念</h2>
                <div class="h-1 bg-indigo-600 flex-1"></div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <p class="text-lg mb-6">在现代应用程序中，尤其是高并发、大数据量的场景中，能够有效地进行并发操作是提升程序性能和用户体验的关键。Java的JUC(Java Util Concurrent)库是专门为并发编程设计的，它提供了一系列强大的工具，帮助我们在多线程环境下轻松处理任务调度、线程池管理、线程同步等问题。</p>
                    <p class="text-lg">通过JUC，我们不需要手动管理线程的创建和销毁，也不需要担心复杂的线程安全问题，它为我们提供了丰富的接口和实现类，简化了并发编程的复杂度。</p>
                </div>
                <div>
                    <img src="https://cdn.nlark.com/yuque/0/2025/png/21449790/1740122332279-e2c49d13-55d8-4fcb-983b-ca8f72ccb14f.png" alt="JUC并发编程" class="rounded-xl shadow-lg w-full h-auto">
                </div>
            </div>
            
            <div class="bg-blue-50 p-8 rounded-xl border border-blue-100">
                <h3 class="text-2xl font-bold mb-4 text-blue-800"><i class="fas fa-lightbulb mr-2 text-yellow-500"></i>为什么要学习JUC?</h3>
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-white p-6 rounded-lg shadow-sm">
                        <div class="text-blue-600 mb-3"><i class="fas fa-bolt text-2xl"></i></div>
                        <h4 class="font-bold mb-2">性能提升</h4>
                        <p class="text-gray-600">充分利用多核CPU的计算能力，大幅提升程序执行效率</p>
                    </div>
                    <div class="bg-white p-6 rounded-lg shadow-sm">
                        <div class="text-blue-600 mb-3"><i class="fas fa-shield-alt text-2xl"></i></div>
                        <h4 class="font-bold mb-2">线程安全</h4>
                        <p class="text-gray-600">提供线程安全的集合和工具类，避免数据竞争和不一致</p>
                    </div>
                    <div class="bg-white p-6 rounded-lg shadow-sm">
                        <div class="text-blue-600 mb-3"><i class="fas fa-cogs text-2xl"></i></div>
                        <h4 class="font-bold mb-2">简化开发</h4>
                        <p class="text-gray-600">高级API封装底层细节，让开发者更专注于业务逻辑</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Core Components Section -->
        <section id="juc-components" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-600 flex-1"></div>
                <h2 class="text-3xl font-bold px-4">JUC核心组件</h2>
                <div class="h-1 bg-indigo-600 flex-1"></div>
            </div>
            
            <div class="mb-12">
                <h3 class="text-2xl font-bold mb-6">JUC的五大核心组件体系</h3>
                <div class="concept-diagram mb-8">
                    <div class="mermaid">
                        graph TD
                            A[JUC核心组件] --> B[线程池框架]
                            A --> C[并发集合]
                            A --> D[同步工具]
                            A --> E[锁机制]
                            A --> F[原子变量]
                            
                            B --> B1[Executor]
                            B --> B2[ExecutorService]
                            B --> B3[ThreadPoolExecutor]
                            B --> B4[ScheduledExecutorService]
                            
                            C --> C1[ConcurrentHashMap]
                            C --> C2[CopyOnWriteArrayList]
                            C --> C3[BlockingQueue]
                            
                            D --> D1[CountDownLatch]
                            D --> D2[CyclicBarrier]
                            D --> D3[Semaphore]
                            
                            E --> E1[ReentrantLock]
                            E --> E2[ReadWriteLock]
                            E --> E3[StampedLock]
                            
                            F --> F1[AtomicInteger]
                            F --> F2[AtomicReference]
                            F --> F3[LongAdder]
                    </div>
                </div>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h4 class="text-xl font-bold mb-4 text-indigo-700">1. 线程池框架 (Executor Framework)</h4>
                        <p class="mb-4">JUC为多线程管理提供了线程池框架，最大程度避免了频繁创建和销毁线程带来的开销。主要接口包括：</p>
                        <ul class="list-disc pl-6 space-y-2">
                            <li><code class="bg-gray-100 px-2 py-1 rounded">Executor</code> - 最基本的执行接口</li>
                            <li><code class="bg-gray-100 px-2 py-1 rounded">ExecutorService</code> - 扩展了Executor，提供生命周期管理</li>
                            <li><code class="bg-gray-100 px-2 py-1 rounded">ThreadPoolExecutor</code> - 可配置的线程池实现</li>
                            <li><code class="bg-gray-100 px-2 py-1 rounded">ScheduledExecutorService</code> - 支持定时任务</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="text-xl font-bold mb-4 text-indigo-700">2. 并发集合 (Concurrent Collections)</h4>
                        <p class="mb-4">JUC提供了多种线程安全的集合类，性能优于传统的同步集合：</p>
                        <ul class="list-disc pl-6 space-y-2">
                            <li><code class="bg-gray-100 px-2 py-1 rounded">ConcurrentHashMap</code> - 分段锁实现的并发哈希表</li>
                            <li><code class="bg-gray-100 px-2 py-1 rounded">CopyOnWriteArrayList</code> - 写时复制的线程安全列表</li>
                            <li><code class="bg-gray-100 px-2 py-1 rounded">BlockingQueue</code> - 支持阻塞操作的队列</li>
                            <li><code class="bg-gray-100 px-2 py-1 rounded">ConcurrentLinkedQueue</code> - 无锁并发队列</li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <div class="text-indigo-600 mb-4"><i class="fas fa-project-diagram text-3xl"></i></div>
                    <h4 class="font-bold text-lg mb-3">同步工具</h4>
                    <p class="text-gray-600 mb-4">协调多线程执行的工具类，包括:</p>
                    <ul class="space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><code class="bg-gray-100 px-1 py-0.5 rounded">CountDownLatch</code> - 倒计时门闩</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><code class="bg-gray-100 px-1 py-0.5 rounded">CyclicBarrier</code> - 循环屏障</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><code class="bg-gray-100 px-1 py-0.5 rounded">Semaphore</code> - 信号量</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <div class="text-indigo-600 mb-4"><i class="fas fa-lock text-3xl"></i></div>
                    <h4 class="font-bold text-lg mb-3">锁机制</h4>
                    <p class="text-gray-600 mb-4">比synchronized更灵活的锁API:</p>
                    <ul class="space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><code class="bg-gray-100 px-1 py-0.5 rounded">ReentrantLock</code> - 可重入锁</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><code class="bg-gray-100 px-1 py-0.5 rounded">ReadWriteLock</code> - 读写分离锁</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><code class="bg-gray-100 px-1 py-0.5 rounded">StampedLock</code> - 乐观读锁</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <div class="text-indigo-600 mb-4"><i class="fas fa-atom text-3xl"></i></div>
                    <h4 class="font-bold text-lg mb-3">原子变量</h4>
                    <p class="text-gray-600 mb-4">基于CAS的无锁线程安全变量:</p>
                    <ul class="space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><code class="bg-gray-100 px-1 py-0.5 rounded">AtomicInteger</code> - 原子整型</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><code class="bg-gray-100 px-1 py-0.5 rounded">AtomicReference</code> - 原子引用</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><code class="bg-gray-100 px-1 py-0.5 rounded">LongAdder</code> - 高并发计数器</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Thread Pool Section -->
        <section id="thread-pool" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-600 flex-1"></div>
                <h2 class="text-3xl font-bold px-4">线程池与任务调度</h2>
                <div class="h-1 bg-indigo-600 flex-1"></div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <h3 class="text-2xl font-bold mb-6">为什么需要线程池？</h3>
                    <div class="space-y-6">
                        <div>
                            <h4 class="font-bold mb-2 flex items-center"><i class="fas fa-microchip text-indigo-500 mr-2"></i>线程创建与销毁开销</h4>
                            <p>在Java中，每个线程的创建都会占用一定资源，频繁创建和销毁线程会给系统带来极大性能损耗，甚至导致"线程风暴"。</p>
                        </div>
                        <div>
                            <h4 class="font-bold mb-2 flex items-center"><i class="fas fa-recycle text-indigo-500 mr-2"></i>线程复用与资源管理</h4>
                            <p>线程池的核心作用是复用已有线程，避免重复创建和销毁线程带来的开销，同时限制线程的并发数量。</p>
                        </div>
                        <div>
                            <h4 class="font-bold mb-2 flex items-center"><i class="fas fa-tasks text-indigo-500 mr-2"></i>任务调度与线程管理</h4>
                            <p>线程池提供高级的任务执行策略，包括任务队列管理、任务优先级控制和任务拒绝策略。</p>
                        </div>
                    </div>
                </div>
                <div>
                    <div class="bg-indigo-50 p-6 rounded-xl border border-indigo-100">
                        <h4 class="font-bold text-lg mb-4 text-indigo-800">Java线程池类型对比</h4>
                        <div class="overflow-x-auto">
                            <table class="min-w-full bg-white rounded-lg overflow-hidden">
                                <thead class="bg-indigo-100">
                                    <tr>
                                        <th class="px-4 py-2 text-left">类型</th>
                                        <th class="px-4 py-2 text-left">特点</th>
                                        <th class="px-4 py-2 text-left">适用场景</th>
                                    </tr>
                                </thead>
                                <tbody class="divide-y divide-gray-200">
                                    <tr>
                                        <td class="px-4 py-3"><code>FixedThreadPool</code></td>
                                        <td class="px-4 py-3">固定大小线程池</td>
                                        <td class="px-4 py-3">任务量稳定的高并发场景</td>
                                    </tr>
                                    <tr class="bg-gray-50">
                                        <td class="px-4 py-3"><code>CachedThreadPool</code></td>
                                        <td class="px-4 py-3">动态调整线程数</td>
                                        <td class="px-4 py-3">短时任务且任务量波动大</td>
                                    </tr>
                                    <tr>
                                        <td class="px-4 py-3"><code>SingleThreadExecutor</code></td>
                                        <td class="px-4 py-3">单线程顺序执行</td>
                                        <td class="px-4 py-3">需要顺序执行任务的场景</td>
                                    </tr>
                                    <tr class="bg-gray-50">
                                        <td class="px-4 py-3"><code>ScheduledThreadPool</code></td>
                                        <td class="px-4 py-3">支持定时任务</td>
                                        <td class="px-4 py-3">周期性任务执行</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="mb-12">
                <h3 class="text-2xl font-bold mb-6">线程池核心参数配置</h3>
                <div class="bg-white p-6 rounded-xl shadow-md">
                    <div class="code-block">
                        <span class="code-comment">// 创建自定义线程池示例</span><br>
                        <span class="code-keyword">ExecutorService</span> executor = <span class="code-keyword">new</span> <span class="code-type">ThreadPoolExecutor</span>(<br>
                        &nbsp;&nbsp;<span class="code-comment">// 核心线程数</span><br>
                        &nbsp;&nbsp;5, <br>
                        &nbsp;&nbsp;<span class="code-comment">// 最大线程数</span><br>
                        &nbsp;&nbsp;10, <br>
                        &nbsp;&nbsp;<span class="code-comment">// 空闲线程存活时间</span><br>
                        &nbsp;&nbsp;60, <span class="code-type">TimeUnit</span>.SECONDS,<br>
                        &nbsp;&nbsp;<span class="code-comment">// 任务队列</span><br>
                        &nbsp;&nbsp;<span class="code-keyword">new</span> <span class="code-type">LinkedBlockingQueue</span>&lt;&gt;(100),<br>
                        &nbsp;&nbsp;<span class="code-comment">// 线程工厂</span><br>
                        &nbsp;&nbsp;<span class="code-type">Executors</span>.defaultThreadFactory(),<br>
                        &nbsp;&nbsp;<span class="code-comment">// 拒绝策略</span><br>
                        &nbsp;&nbsp;<span class="code-keyword">new</span> <span class="code-type">ThreadPoolExecutor</span>.AbortPolicy()<br>
                        );
                    </div>
                </div>
            </div>
        </section>

        <!-- Concurrency Concepts Section -->
        <section id="concurrency-concepts" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-600 flex-1"></div>
                <h2 class="text-3xl font-bold px-4">并发编程基础概念</h2>
                <div class="h-1 bg-indigo-600 flex-1"></div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <img src="https://cdn.nlark.com/yuque/0/2025/png/21449790/1740122460429-b395e58d-5d77-4446-b70e-d5181f0614e2.png" alt="并发编程概念" class="rounded-xl shadow-lg w-full h-auto">
                </div>
                <div>
                    <div class="mb-8">
                        <h3 class="text-xl font-bold mb-3 text-indigo-700">并发 vs. 并行</h3>
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <div class="flex items-start mb-2">
                                <div class="bg-blue-100 p-2 rounded-full mr-3"><i class="fas fa-random text-blue-600"></i></div>
                                <div>
                                    <h4 class="font-bold">并发 (Concurrency)</h4>
                                    <p class="text-gray-600">单个处理器上多个任务交替执行，看起来像是同时发生</p>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <div class="bg-green-100 p-2 rounded-full mr-3"><i class="fas fa-parallel text-green-600"></i></div>
                                <div>
                                    <h4 class="font-bold">并行 (Parallelism)</h4>
                                    <p class="text-gray-600">多核处理器上多个任务真正同时执行</p>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div>
                        <h3 class="text-xl font-bold mb-3 text-indigo-700">线程 vs. 进程</h3>
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <div class="flex items-start mb-2">
                                <div class="bg-purple-100 p-2 rounded-full mr-3"><i class="fas fa-boxes text-purple-600"></i></div>
                                <div>
                                    <h4 class="font-bold">进程 (Process)</h4>
                                    <p class="text-gray-600">操作系统分配资源的基本单位，有独立内存空间</p>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <div class="bg-pink-100 p-2 rounded-full mr-3"><i class="fas fa-code-branch text-pink-600"></i></div>
                                <div>
                                    <h4 class="font-bold">线程 (Thread)</h4>
                                    <p class="text-gray-600">进程中的执行单元，共享进程资源，更轻量级</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white p-6 rounded-xl shadow-sm border-l-4 border-blue-500">
                    <h3 class="font-bold text-lg mb-3">同步 vs. 异步</h3>
                    <ul class="space-y-3">
                        <li>
                            <span class="font-semibold text-blue-600">同步:</span> 
                            <span class="text-gray-600">阻塞式操作，线程必须等待任务完成</span>
                        </li>
                        <li>
                            <span class="font-semibold text-blue-600">异步:</span> 
                            <span class="text-gray-600">非阻塞式操作，线程继续执行其他任务</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-sm border-l-4 border-green-500">
                    <h3 class="font-bold text-lg mb-3">线程安全</h3>
                    <p class="text-gray-600 mb-3">多线程访问共享资源时不导致数据不一致</p>
                    <div class="text-sm">
                        <span class="inline-block bg-green-100 text-green-800 px-2 py-1 rounded mr-2">synchronized</span>
                        <span class="inline-block bg-green-100 text-green-800 px-2 py-1 rounded mr-2">ReentrantLock</span>
                        <span class="inline-block bg-green-100 text-green-800 px-2 py-1 rounded">Atomic类</span>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-sm border-l-4 border-purple-500">
                    <h3 class="font-bold text-lg mb-3">死锁与竞态条件</h3>
                    <ul class="space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-skull-crossbones text-red-500 mt-1 mr-2"></i>
                            <span><span class="font-semibold">死锁:</span> 线程互相等待对方释放资源</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-running text-yellow-500 mt-1 mr-2"></i>
                            <span><span class="font-semibold">竞态条件:</span> 执行顺序影响结果</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Advanced Topics Section -->
        <section id="advanced-topics" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-600 flex-1"></div>
                <h2 class="text-3xl font-bold px-4">高级主题与最佳实践</h2>
                <div class="h-1 bg-indigo-600 flex-1"></div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <h3 class="text-2xl font-bold mb-6">锁优化策略</h3>
                    
                    <div class="mb-8">
                        <h4 class="text-xl font-bold mb-3 text-indigo-700">1. 锁分离与细粒度锁</h4>
                        <p class="mb-4">通过将资源分成多个段，每个段有独立的锁，减少锁竞争：</p>
                        <div class="code-block">
                            <span class="code-comment">// ConcurrentHashMap的分段锁实现</span><br>
                            <span class="code-keyword">final</span> <span class="code-type">Segment</span>&lt;K,V>[] segments;<br>
                            <span class="code-keyword">static final class</span> <span class="code-type">Segment</span>&lt;K,V> <span class="code-keyword">extends</span> <span class="code-type">ReentrantLock</span> {<br>
                            &nbsp;&nbsp;<span class="code-comment">// 每个Segment独立加锁</span><br>
                            &nbsp;&nbsp;<span class="code-type">transient volatile</span> <span class="code-type">HashEntry</span>&lt;K,V>[] table;<br>
                            }
                        </div>
                    </div>
                    
                    <div>
                        <h4 class="text-xl font-bold mb-3 text-indigo-700">2. 读写锁 (ReadWriteLock)</h4>
                        <p class="mb-4">适用于读多写少的场景，提高并发性能：</p>
                        <div class="code-block">
                            <span class="code-type">ReadWriteLock</span> lock = <span class="code-keyword">new</span> <span class="code-type">ReentrantReadWriteLock</span>();<br>
                            <span class="code-type">Lock</span> readLock = lock.readLock(); <span class="code-comment">// 共享锁</span><br>
                            <span class="code-type">Lock</span> writeLock = lock.writeLock(); <span class="code-comment">// 排他锁</span>
                        </div>
                    </div>
                </div>
                
                <div>
                    <h3 class="text-2xl font-bold mb-6">CAS与无锁编程</h3>
                    
                    <div class="mb-8">
                        <h4 class="text-xl font-bold mb-3 text-indigo-700">Compare-And-Swap (CAS)</h4>
                        <p class="mb-4">原子操作，无需加锁实现线程安全：</p>
                        <div class="code-block">
                            <span class="code-keyword">public final</span> <span class="code-keyword">boolean</span> <span class="code-type">compareAndSet</span>(<span class="code-keyword">int</span> expect, <span class="code-keyword">int</span> update) {<br>
                            &nbsp;&nbsp;<span class="code-comment">// 比较并交换的底层实现</span><br>
                            &nbsp;&nbsp;<span class="code-keyword">return</span> unsafe.compareAndSwapInt(<span class="code-keyword">this</span>, valueOffset, expect, update);<br>
                            }
                        </div>
                        <p class="mt-4 text-gray-600">Java中的<code class="bg-gray-100 px-1 py-0.5 rounded">AtomicInteger</code>、<code class="bg-gray-100 px-1 py-0.5 rounded">AtomicReference</code>等类都是基于CAS实现。</p>
                    </div>
                    
                    <div>
                        <h4 class="text-xl font-bold mb-3 text-indigo-700">LongAdder高性能计数器</h4>
                        <p class="mb-4">在高并发环境下比AtomicLong性能更好：</p>
                        <div class="code-block">
                            <span class="code-type">LongAdder</span> counter = <span class="code-keyword">new</span> <span class="code-type">LongAdder</span>();<br>
                            counter.increment(); <span class="code-comment">// 线程安全的自增</span><br>
                            <span class="code-keyword">long</span> sum = counter.sum(); <span class="code-comment">// 获取当前值</span>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="bg-yellow-50 p-8 rounded-xl border border-yellow-200">
                <h3 class="text-2xl font-bold mb-6 text-yellow-800"><i class="fas fa-exclamation-triangle mr-2"></i>并发编程最佳实践</h3>
                <div class="grid md:grid-cols-2 gap-6">
                    <div>
                        <h4 class="font-bold mb-3 text-lg">1. 避免死锁</h4>
                        <ul class="space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>按固定顺序获取多个锁</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>使用带超时的锁 (tryLock)</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>减少锁的持有时间</span>
                            </li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="font-bold mb-3 text-lg">2. 性能优化</h4>
                        <ul class="space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>减少锁的粒度 (减小临界区)</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>读写分离 (ReadWriteLock)</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>优先使用并发集合类</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-10 px-4">
        <div class="max-w-6xl mx-auto">
            <div class="text-center">
                <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                <p class="mb-4">专注于高质量的技术内容分享</p>
                <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition duration-300">
                    <i class="fas fa-external-link-alt mr-1"></i> www.yuque.com/jtostring
                </a>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-sm">
                <p>© 2023 技术小馆. All rights reserved.</p>
            </div>
        </div>
    </footer>

    <!-- Mermaid JS for diagrams -->
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>